
/**
 * name:        个人中心绑定手机号
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   bgemall.com
 */

// 会员个人中心首页
$(function(){
    // 个人中心首页 

    
    if($.cookie('user')!=''&&$.cookie('user')!=null){
        var token='';
        isLogin();     
        getToken();     // 获取/验证token 
        if( token!='' && token!=null){
            getMemberBasicInfo(token,URL);
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2')
        var nav=''
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>'
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>'
        nav+='<i class="icon_down"></i>'
        nav+='<ul class="logined">'
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>'
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>'
        nav+='</ul>'
        $('.mod_login').append(nav);
    }

    // 获取/验证token
    function getToken(){
        strs=$.cookie('user').split(",");
        for(var i in strs){ 
            $('.nike_name').html(strs[1]) 
            token=strs[6];
            name=strs[1];
        }
    }
    // 退出登录
    $('.login_out').click(function(){
        $.cookie('user', null, { path: '/' });
        $.ajax({
            type:'post',
            url:new_url+'shop/login/logOut',
            dataType:'json',
            data:'',
            success:function(data){
                tc(data.message)
            }  
        })
        setTimeout(function(){
            window.location.href='/index.html'
        },1100);
    });
    // 获取验证码
    $(".send_code").click(function(){
        var phone =$(".phone_input_default").val();
        $.ajax({
            type:'post',
            url:new_url+'shop/login/sendPhoneCode',
            dataType:'json',
            data:{phone:phone},  
            success:function(e){ 
                if(e.code!='0'){
                    tc(e.message);
                }else{
                    tc(e.message);
                    var time='60';
                    function tim(){
                        setTimeout(function(){                                               
                        if(time>'0'){
                            tim();
                            time --
                            $('.send_code').text(time+'s重新获取');
                            $('.send_code').css('cursor','default');
                        }
                        else{
                            $('.send_code').text('重新获取验证码');
                            $('.send_code').css('cursor','pointer');
                        }    
                        },1000)
                    }
                    tim();
                }
            }
        });
    });
    // 身份验证
    $("#next_two").click(function(){
        var phoneDefault = $("phone_input_default").val();
        var phoneCode= $(".code_input").val();
        if(phoneDefault !='' || phoneCode !=''){
            $.ajax({
                type:'post',
                url:new_url+'shop/home/verifyPhone',
                dataType:'json',
                data:{token:token,phoneCode:phoneCode},  
                success:function(e){ 
                    if(e.code=='0'){ 
                        setTimeout(function(){
                            window.location.href='member_bind_phone_two.html';
                        },1000)
                    }else{
                        tc(e.message);
                        
                    } 
                }
            });
        }else{
            tc("验证码不能为空");
        }
    });
    // 修改手机号
    $("#next_three").click(function(){
        var phone = $(".phone_input").val();
        var phoneCode= $(".code_input").val(); 
        if(phone !='' || phoneCode !=''){
            $.ajax({
                type:'post',
                url:new_url+'shop/home/bindPhone',
                dataType:'json',
                data:{token:token,phone:phone,phoneCode:phoneCode},  
                success:function(e){         
                    if(e.code=='0'){
                        strs=$.cookie('user').split(",")
                        var name=''
                        var nickName=''
                        var phone_binding=''
                        var avatar=''
                        var truename=''
                        var memberId=''
                        var token=''
                        for(var i in strs){
                            name=strs[0]
                            nickName=strs[1]
                            avatar=strs[3]
                            truename=strs[4]
                            memberId=strs[5]
                            token=strs[6]
                            // phone_binding=strs[2]
                        }

                        var mycars = new Array()
                        mycars[0] =name
                        mycars[1] =nickName
                        mycars[2] =phone
                        mycars[3] =avatar
                        mycars[4] =truename
                        mycars[5] =memberId
                        mycars[6] =token
                        mycars[7] = 
                        $.cookie('user',mycars,{path:'/'});
                        setTimeout(function(){
                            window.location.href='member_bind_phone_three.html';
                        },1000);
                        
                    }else{
                        tc(e.message);
                        
                    } 
                }
            });
        }else{
            tc("验证码不能为空");
        }
    });

});


function getMemberBasicInfo(token,URL){
    $.ajax({
        type:'post',
        url:new_url+'shop/member/getMemberInfo',
        dataType:'json',
        data:{token:token},  
        success:function(e){
            if(e.code=='0'){
                $(".phone_input_default").val(e.data.phone);
                $("#new_phone").text(e.data.phone)
            }else{
                tc(e.message);
                setTimeout(function(){
                        window.location.href='/src/login/login.html?type=2'
                },1100)
            } 
        }
    });
}